package com.woyowodaows.activity.home;

import android.content.Intent;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Vibrator;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.woyowodaows.R;
import com.woyowodaows.activity.MainActivity;
import com.woyowodaows.activity.mine.FindPasswordActivity;
import com.woyowodaows.activity.mine.RegisterActivity;
import com.woyowodaows.application.MyApplication;
import com.woyowodaows.base.BaseActivity;
import com.woyowodaows.bean.home.UserDataBean;
import com.woyowodaows.request.HttpAsyncTask;
import com.woyowodaows.request.HttpRequestListener;
import com.woyowodaows.request.InterfaceUrl;
import com.woyowodaows.utils.CacheUserInfoUtils;
import com.woyowodaows.utils.JpushTagData;
import com.woyowodaows.utils.SPUtils;
import com.woyowodaows.utils.ToastUtil;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;

import static com.woyowodaows.application.MyApplication.getApplicationContext;

public class LoginActivity extends BaseActivity {

    @BindView(R.id.imageView2)
    ImageView imageView2;
    @BindView(R.id.et_login_phone)
    EditText etLoginPhone;
    @BindView(R.id.imageView)
    ImageView imageView;
    @BindView(R.id.et_login_pwd)
    EditText etLoginPwd;
    @BindView(R.id.ll_login_heard)
    LinearLayout llLoginHeard;
    @BindView(R.id.tv_login_forget)
    TextView tvLoginForget;
    @BindView(R.id.ll_login_forget)
    LinearLayout llLoginForget;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.tv_login_register)
    TextView tvLoginRegister;
    @BindView(R.id.ll_register)
    LinearLayout llRegister;
    @BindView(R.id.tv_login_winxi)
    TextView tvLoginWinxi;
    @BindView(R.id.tv_login_qq)
    TextView tvLoginQq;

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
        super.initView();
        ButterKnife.bind(this);
        setTitleBar(false, "登录", "", 0, 0, null);
    }

    @Override
    public void initEvent() {
        super.initEvent();

    }

    private void loginUser() {


        final String userName = etLoginPhone.getText().toString().trim();
        final String password = etLoginPwd.getText().toString().trim();

        if (TextUtils.isEmpty(userName)) {
            ToastUtil.show_short(mContext, "账号不能为空");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            ToastUtil.show_short(mContext, "密码不能为空");
            return;
        }
        Map<String, Object> mapParameter = new HashMap<>();
        mapParameter.put("lonpwd", password);
        mapParameter.put("phone", userName);//18812345678
        HttpAsyncTask.IS_LOGIN = true;
        showProgressDialog(mContext, "登录中...");
        HttpAsyncTask.getInstance().onPostParam(this, "", false, InterfaceUrl.LOGIN_URL, UserDataBean.class, mapParameter, new HttpRequestListener() {
            @Override
            public void onSuccess(Object result) {
                if (result != null) {
                    UserDataBean bean = (UserDataBean) result;
                    if (bean.getResult() == HttpAsyncTask.SUCCESS) {
                        if (bean.getData() != null) {
                            SPUtils.put(mContext, SPUtils.IS_LOGIN, true);
                            ToastUtil.show_short(mContext, "登录成功");
                            bean.getData().setAvatar(bean.getFileUrl()+bean.getData().getAvatar());
                            CacheUserInfoUtils.setUserInfo(bean.getData());
//                            startActivity(new Intent(mContext, MainActivity.class));
                            finish();
                        }
                    } else {
                        ToastUtil.show_short(LoginActivity.this, bean.getMessage());
                    }
                }
                dismissProgressDialog();
            }

            @Override
            public void onError(String message) {
                dismissProgressDialog();
                ToastUtil.show_short(LoginActivity.this, message);
            }
        });

    }


    @OnClick({R.id.ll_login_forget, R.id.btn_login, R.id.ll_register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_login_forget://忘记密码
                startActivity(new Intent(mContext, FindPasswordActivity.class));
                break;
            case R.id.btn_login://登录
                loginUser();
                break;
            case R.id.ll_register://注册
                startActivity(new Intent(mContext, RegisterActivity.class));
                break;
        }
    }
    /**
     * 设置标签与别名
     */
    private void setTagAndAlias() {
        /**
         *这里设置了别名，在这里获取的用户登录的信息
         *并且此时已经获取了用户的userId,然后就可以用用户的userId来设置别名了
         **/
        //false状态为未设置标签与别名成功
        //if (UserUtils.getTagAlias(getHoldingActivity()) == false) {
//        Set<String> tags = new HashSet<String>();
//        //这里可以设置你要推送的人，一般是用户uid 不为空在设置进去 可同时添加多个
//        if (!TextUtils.isEmpty(UserUtils.getUid(getHoldingActivity()))){
//            tags.add(UserUtils.getUid(getHoldingActivity()));//设置tag
//        }
        //上下文、别名【Sting行】、标签【Set型】、回调
//        JPushInterface.setAliasAndTags(getHoldingActivity(), UserUtils.getUid(getHoldingActivity()), tags,
//                mAliasCallback);
//        // }

    }

}
